package com.songcha.library_business.ui.adapter

import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout.LayoutParams
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.marginLeft
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.songcha.library_business.R
import com.songcha.library_business.bean.book.BookNetBean
import com.songcha.library_business.handler.BookStatusHandler
import com.songcha.library_business.interfaces.IOnBookClickListener
import com.songcha.library_common.helper.ViewHelper
import com.songcha.library_common.ui.view.SmallDecimalView
import com.songcha.library_common.util.GlideUtil
import com.songcha.library_common.util.LogUtil
import com.songcha.library_common.util.StringUtil
import com.songcha.library_common.util.dp2px


class BookListAdapter2(list:MutableList<BookNetBean>, val listener:IOnBookClickListener, val showRank:Boolean=true, val showTextCount:Boolean=false):BaseQuickAdapter<BookNetBean,BaseViewHolder>(R.layout.business_item_book_rank2,list) {
    companion object{
        const val MAX_SHOW_RANK=3
    }

    init {
        setOnItemClickListener{a,v,p->
            val iv = v.findViewById<ImageView>(R.id.business_item_iv_thumb)
            listener.onBookClick(list[p].bookId.toString(),iv)
        }
    }

    override fun convert(holder: BaseViewHolder, item: BookNetBean) {
        val iv = holder.getView<ImageView>(R.id.business_item_iv_thumb)
        val tvBookName = holder.getView<TextView>(R.id.business_item_tv_book_name)
        val tvDesc = holder.getView<TextView>(R.id.business_item_tv_desc)
        val tvAuthor = holder.getView<TextView>(R.id.business_item_tv_author)
        val btnStartRead = holder.getView<TextView>(R.id.business_btn_start_read)
        val ivRank = holder.getView<ImageView>(R.id.business_item_iv_rank)

        tvBookName.text = item.bookName
        tvDesc.text = item.description
        tvAuthor.text = item.author
        ViewHelper.setVisibility(false,btnStartRead)
        //TODO
        GlideUtil.loadNetImage(iv,item.cover)

        if(showRank){
            ViewHelper.setVisibility(holder.absoluteAdapterPosition < 3,ivRank)
            if(holder.absoluteAdapterPosition == 0){
                ivRank.setImageResource(R.mipmap.business_icon_book_list_rank_1)
            }else  if(holder.absoluteAdapterPosition == 1) {
                ivRank.setImageResource(R.mipmap.business_icon_book_list_rank_2)
            } else  if(holder.absoluteAdapterPosition == 2) {
                ivRank.setImageResource(R.mipmap.business_icon_book_list_rank_3)
            }
        }else{
            ViewHelper.setVisibility(false,ivRank)
        }



        /*val tvBookName=holder.getView<TextView>(R.id.business_tv_bookName).apply {
            text=item.bookName
        }

        holder.getView<TextView>(R.id.business_tv_rank).apply {
            if(!showRank && showTextCount){
                val lp=layoutParams as ConstraintLayout.LayoutParams
                lp.topMargin=0
                layoutParams=lp
            }else{
                val lp=layoutParams as ConstraintLayout.LayoutParams
                lp.topMargin=5.dp2px()
                layoutParams=lp
            }

            if(!showRank || holder.absoluteAdapterPosition+1>MAX_SHOW_RANK){
                text=""
                visibility= View.INVISIBLE
                tvBookName.layoutParams=tvBookName.layoutParams.apply {
                    this as ConstraintLayout.LayoutParams
                    leftMargin=0
                    topToTop = ConstraintLayout.LayoutParams.PARENT_ID
                    baselineToBaseline = ConstraintLayout.LayoutParams.UNSET
                }
            }else{
                text=(holder.adapterPosition+1).toString()
                visibility= View.VISIBLE
                tvBookName.layoutParams=tvBookName.layoutParams.apply {
                    this as ConstraintLayout.LayoutParams
                    leftMargin=8.dp2px()
                    topToTop = ConstraintLayout.LayoutParams.UNSET
                    baselineToBaseline = R.id.business_tv_rank

                }
            }

        }

        holder.getView<TextView>(R.id.business_tv_textCount).apply {
            if(!showRank && showTextCount){
                visibility=View.VISIBLE
                val bookStatus = BookStatusHandler.handle(item.status)
                text="${bookStatus}・${StringUtil.float2PlainString (item.words/10000f)}万字"
            }else{
                visibility=View.GONE
            }
        }

        holder.getView<TextView>(R.id.business_tv_author).apply {
            text=item.author
        }

        holder.getView<TextView>(R.id.business_tv_desc).apply {
            text=item.description
        }

        holder.getView<SmallDecimalView>(R.id.business_smv_score).apply {
            setNumber(item.averageRating)
        }

        holder.getView<ImageView>(R.id.business_iv_thumb).apply {
            GlideUtil.loadNetImage(this,item.cover)
        }

        val clParent=holder.getView<ConstraintLayout>(R.id.business_book_item_cl_parent)

        if(holder.absoluteAdapterPosition==0){
            clParent.layoutParams=clParent.layoutParams.apply {
                this as ViewGroup.MarginLayoutParams
                topMargin=10.dp2px()
                bottomMargin=5.dp2px()
            }
        }else if(holder.absoluteAdapterPosition==data.size){
            clParent.layoutParams=clParent.layoutParams.apply {
                this as ViewGroup.MarginLayoutParams
                bottomMargin=10.dp2px()
                topMargin=5.dp2px()
            }
        }else{
            clParent.layoutParams=clParent.layoutParams.apply {
                this as ViewGroup.MarginLayoutParams
                topMargin=5.dp2px()
                bottomMargin=5.dp2px()
            }
        }*/

    }
}